package ikar.nrd.fillingref.generator;

import ikar.nrd.model.FillingRef;
import ikar.nrd.model.XmlNamespaceManager;

import java.util.ArrayList;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class NewFillingRefProcessor extends CoreScriptGenerator {

	public NewFillingRefProcessor(String patternId) {
		super(patternId);
	}

	private final List<String> existingPlateCodes = new ArrayList<String>();

	public void loadXmlSchemasTree(Document doc, DefaultMutableTreeNode rootTreeNode) {
		rollOverNode(doc.getDocumentElement(), new FillingRef(), rootTreeNode);
	}

	protected void rollOverNode(Node parentNode, FillingRef parentFillingRef, DefaultMutableTreeNode rootNode) {
		NodeList nodeList = parentNode.getChildNodes();
		List<String> attrQueryList = new ArrayList<String>();
		for (int i = 0; i < nodeList.getLength(); i++) {
			List<DefaultMutableTreeNode> attributesTreeNodes = new ArrayList<DefaultMutableTreeNode>();
			FillingRef fillingRefRow = new FillingRef();
			DefaultMutableTreeNode currentTreeNode = new DefaultMutableTreeNode(fillingRefRow);
			String elementSchema = null;
			fillingRefRow.setSelected(true);

			Node currentNode = nodeList.item(i);

			attrQueryList.clear();
			String currentNodeClearValue = getClearValue(nodeList.item(i).getNodeName());
			if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
				parentFillingRef.setSelected(false);
				String currentElementPlateCode = parentFillingRef.getPlateCode() != null ? parentFillingRef.getPlateCode() + "_" + currentNodeClearValue.toUpperCase() : currentNodeClearValue.toUpperCase();
				fillingRefRow.setFullPath(getFullPath(parentFillingRef, currentNode));

				if (existingPlateCodes.contains(currentElementPlateCode)) {
					// rollOverNode(currentNode, currentElementPlateCode, rootNode);
					continue;
				} else {
					existingPlateCodes.add(currentElementPlateCode);
				}
				for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
					String attributeName = "@" + currentNode.getAttributes().item(j).getNodeName();
					if (attributeName.contains("IdScheme")) {
						elementSchema = currentNode.getAttributes().item(j).getNodeValue();
						continue;
					}
					String attrPlateCode = currentElementPlateCode + "_" + attributeName.replace("@", "").toUpperCase();

					FillingRef attrFillingRefRow = new FillingRef();
					attrFillingRefRow.setXpath(attributeName);
					attrFillingRefRow.setPlateCode(attrPlateCode);
					attrFillingRefRow.setParentPlateCode(currentElementPlateCode);
					attrFillingRefRow.setAttribute(true);
					attrFillingRefRow.setSelected(true);
					attrFillingRefRow.setFullPath(CoreUtils.isEmpty(fillingRefRow.getFullPath()) ? attributeName : fillingRefRow.getFullPath() + "/" + attributeName);
					attributesTreeNodes.add(new DefaultMutableTreeNode(attrFillingRefRow));
					fillingRefRow.setSelected(false);

				}

				if (elementSchema != null) {
					currentNodeClearValue += "[" + currentNodeClearValue + "Scheme=''" + elementSchema + "'']";
				}

				if (currentElementPlateCode != null && currentElementPlateCode.length() > 0) {
					fillingRefRow.setXpath(currentNodeClearValue);
					fillingRefRow.setPlateCode(currentElementPlateCode);
					fillingRefRow.setParentPlateCode(parentFillingRef.getPlateCode());
					for (DefaultMutableTreeNode attribute : attributesTreeNodes) {
						currentTreeNode.add(attribute);
					}
					rootNode.add(currentTreeNode);
				}
				rollOverNode(currentNode, fillingRefRow, currentTreeNode);
			}
		}
	}

	private String getFullPath(FillingRef parentFillingRef, Node currentNode) {
		String nodePrefix = XmlNamespaceManager.getLinkByUri(currentNode.getNamespaceURI());
		String currentXpath = nodePrefix + ":" + getClearValue(currentNode.getNodeName());
		return CoreUtils.isEmpty(parentFillingRef.getFullPath()) ? currentXpath : parentFillingRef.getFullPath() + "/" + currentXpath;
	}

}
